package extractor;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.MemoryHandler;
import java.util.logging.SimpleFormatter;

public class MyLogger {
        private static Boolean logging = false;
        
        static public void setup(final Level trigger, Level level) {
                Logger logger = Logger.getLogger("");
                logger.setLevel(level);
                try {
                        String path = MyLogger.class.getProtectionDomain().getCodeSource().getLocation().getPath();
                        File file = new File(path);
                        String parent = file.getParent();
                        FileHandler logFile = new FileHandler(parent + "/log.txt", true);
                        SimpleFormatter formatterTxt = new SimpleFormatter();
                        logFile.setFormatter(formatterTxt);
                        
                        int numRec = 100;
                    MemoryHandler logMemory = new MemoryHandler(logFile, numRec, Level.OFF) {
                        public synchronized void publish(LogRecord record) {
                            super.publish(record);

                            if (record.getLevel().intValue()>=trigger.intValue() || keepLogging()) {
                                toggleLoggingOn();
                                push();
                            }
                        }
                    };
                        logger.addHandler(logMemory);
                } catch (SecurityException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        
        private static Boolean keepLogging() {
                return logging;
        }
        
        private static void toggleLoggingOn() {
                logging = true;
        }
}
